using Microsoft.AspNetCore.Mvc;

namespace StateManagement.Controllers {
  public class CookiesController : Controller {
    [HttpPost]
    public IActionResult SetUserPreferences(string theme) {
      CookieOptions options = new CookieOptions {
        Expires = DateTimeOffset.UtcNow.AddDays(30),
        //Expires = DateTimeOffset.UtcNow.AddMinutes(2),
        Secure = true, // Ensures cookie is only sent over HTTPS
        HttpOnly = true, // Not accessible via JavaScript
      };
      Response.Cookies.Append("UserTheme", theme, options);
      return RedirectToAction("Index");
    }

    [HttpGet]
    public IActionResult Index() {
      var userTheme = Request.Cookies["UserTheme"] ?? "Default Theme";
      ViewData["Theme"] = userTheme;
      return View();
    }

  }
}
